package handler

import (
	"fmt"
	"ginadmin/models"
	"ginadmin/routes/context"
	"ginadmin/utils"
	"github.com/gin-gonic/gin"
	"time"
)

type MenuHandler struct {
}

func (menu *MenuHandler) Router(group *gin.RouterGroup) {
	group.GET("/menu/list", menu.List)
	group.POST("/menu/add", menu.Add)
	group.POST("/menu/del", menu.Del)
	group.POST("/menu/update", menu.Update)

	group.GET("/menu/indexmenulist", menu.IndexMenuList)

}

func (menu *MenuHandler) Update(c *gin.Context) {

	request := c.Request
	mid := request.FormValue("menuid")
	name := request.FormValue("name")
	url := request.FormValue("url")

	fmt.Println("mid============>", mid)

	if mid == "" {
		context.Err(c, 400, "菜单id不能为空")
		return
	}

	if name == "" {
		context.Err(c, 400, "菜单名称不能为空")
		return
	}

	if url == "" {
		context.Err(c, 400, "菜单url不能为空")
		return
	}
	m, err := models.FindMenuById(mid)
	if err != nil {
		context.Map(c, context.M{"code": 401, "msg": err.Error()})
		return
	}

	err = m.UpdateMenu()
	if err != nil {
		context.Map(c, context.M{"code": 401, "msg": err.Error()})
		return
	}
	context.OK(c)
}

type MenuJsonArray struct {
	Menutitle string    `json:"menutitle"`
	Submenu   []Submenu `json:"submenu"`
}
type SubSubmenu struct {
	Menutitle string `json:"menutitle"`
	URL       string `json:"url"`
}
type Submenu struct {
	Menutitle  string       `json:"menutitle"`
	URL        string       `json:"url,omitempty"`
	SubSubmenu []SubSubmenu `json:"submenu,omitempty"`
}

func (menu *MenuHandler) IndexMenuList(c *gin.Context) {
	//先查parentId为0的
	topLevelMenuList, _ := models.MenuListByParentId(0)
	menuarray := make([]MenuJsonArray, len(topLevelMenuList))
	fmt.Println(time.Now())
	for i, topMenu := range topLevelMenuList {
		//menutitle := topMenu.Name
		subMenuList, _ := models.MenuListByParentId(topMenu.ID)
		submenuarray := make([]Submenu, len(subMenuList))
		for j, submenu := range subMenuList {

			subsubMenuList, _ := models.MenuListByParentId(submenu.ID)
			subsubmenuarray := make([]SubSubmenu, len(subsubMenuList))
			for k, subsubmenu := range subsubMenuList {
				subsubmenuarray[k] = SubSubmenu{Menutitle: subsubmenu.Name, URL: subsubmenu.Url}
			}
			submenuarray[j] = Submenu{Menutitle: submenu.Name, URL: submenu.Url, SubSubmenu: subsubmenuarray}
		}
		menujson := MenuJsonArray{Menutitle: topMenu.Name, Submenu: submenuarray}
		menuarray[i] = menujson
	}
	fmt.Println(time.Now())
	context.Map(c, context.M{"code": 200, "msg": "OK", "data": menuarray})

}

func (menu *MenuHandler) List(c *gin.Context) {
	orgList, err := models.MenuList(0) //0 默认查询全部
	if err != nil {
		context.Map(c, context.M{"code": 401, "msg": err.Error(), "data": nil})
		return
	}
	context.Map(c, context.M{"code": 200, "msg": "OK", "data": orgList})
}

func (menu *MenuHandler) Add(c *gin.Context) {

	request := c.Request
	mid := request.FormValue("menu")
	name := request.FormValue("name")
	url := request.FormValue("url")

	if mid == "" {
		mid = "0"
	} else {
		if url == "" {
			context.Err(c, 400, "菜单url不能为空")
			return
		}
	}

	if name == "" {
		context.Err(c, 400, "菜单名称不能为空")
		return
	}

	m := models.Menu{Name: name, ParentId: utils.StrToUInt(mid), Url: url}
	m.Add()
	context.OK(c)
}

func (menu *MenuHandler) Del(c *gin.Context) {

	request := c.Request
	mid := request.FormValue("menu")

	if mid == "" {
		context.Err(c, 400, "菜单id不能为空")
		return
	}
	menulist, err := models.MenuList(utils.StrToUInt(mid))
	if err != nil {
		context.Map(c, context.M{"code": 401, "msg": err.Error()})
		return
	}
	fmt.Println(len(menulist))
	fmt.Println(utils.StrToUInt(mid))
	if len(menulist) > 0 {
		context.Err(c, 400, "存在下级菜单不能删除")
		return
	}
	err = models.DelMenu(utils.StrToUInt(mid))
	if err != nil {
		context.Err(c, 400, err.Error())
		return
	}

	context.OK(c)
}
